package com.lambda.lambda.wx.domain;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Component
public class WeChatUtil {

    //获取的access_token的url路径
    private static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    //用于存储access_token的对象
    private static WxAccessToken wxAccessToken;

    @Value("${weChat.appid}")
    private String APPID;

    @Value("${weChat.secret}")
    private String APPSECRET;
    /**
     * 对字符串进行sha1加密
     * @param src
     * @return
     */
    public static String sha1Hex(String src){
        try{
            //获取一个加密对象
            MessageDigest messageDigest = MessageDigest.getInstance("sha1");
            //加密
            byte[] digest = messageDigest.digest(src.getBytes());
            char[] chars= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
            //处理加密结果
            StringBuilder sb = new StringBuilder();
            //处理加密结果
            for(byte b:digest) {
                sb.append(chars[(b>>4)&15]);
                sb.append(chars[b&15]);
            }
            return sb.toString();
        }catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

}
